<template>
  <a-modal title-align="start" title="移交任务" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="400">
    <div>请选择任务节点执行人：</div>

    <a-select placeholder="请选择" allow-search v-model="userId">
      <a-option v-for="(item, index) of userList" :key="index" :value="item.id">{{ item.userName }}</a-option>
    </a-select>

    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue'
  import { getOrganizesUser } from '@/api/system'
  import { transferTaskNodes } from '@/api/task'
  import { Message } from '@arco-design/web-vue'
  let props = defineProps(['modelValue', 'type', 'list'])
  let emits = defineEmits(['save', 'update:modelValue'])
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })
  let userList: any = ref([])
  let userId = ref('')
  const getList = async () => {
    let params = {
      depart: false,
      needRole: false,
      pageNum: 1,
      pageSize: 1000,
    }
    const res: any = await getOrganizesUser(params)
    if (res) {
      userList.value = res.data.records || []
    }
  }
  const submit = async () => {
    if (!userId.value) return Message.error('请选择任务移交执行人')
    let data = userList.value.find((item: any) => item.id === userId.value)
    let params = {
      executer: {
        name: data.userName,
        id: data.id,
      },
      keyList: props.list,
    }
    const res: any = await transferTaskNodes(params)
    if (res) {
      Message.success('移交成功')
      emits('save')
      showvisible.value = false
    }
  }
  getList()
</script>

<style lang="less" scoped></style>
